﻿using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace my.cache
{
    public class MemCache : ICache
    {
        private readonly CacheManager cache = new CacheManager();

        public async Task<string> GetAsync(string key)
        {
            var result = cache.Get<string>(key);

            return await Task.FromResult(result);
        }

        public async Task<T> GetAsync<T>(string key) where T : new()
        {
            var result = cache.Get<T>(key);
            return await Task.FromResult(result);
        }

        public Task SetAsync(string key, object value, int expireSeconds = 0)
        {
            if (expireSeconds == 0)
            {
                cache.Set_NotExpire(key, value);
                return Task.CompletedTask;
            }
            cache.Set_AbsoluteExpire(key, value, new TimeSpan(0, 0, expireSeconds));

            return Task.CompletedTask;
        }
    }
}
